/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lista;

/**
 *
 * @author Administrador
 */
public class List {
    private Node first = null;
    private Node last = null;

    public List () {
    }

    public List (Node first) {
        this.first = first;
        this.last = first;
    }

    public Node getFirst() {
        return first;
    }
    public Node getLast() {
        return last;
    }

    public void add(Node node){
        if (this.empty()) {
            this.first = node;
        } else {
            this.last.next = node;
        }
        this.last = node;
    }

    public void add(Node node, int pos) {
        Node nodeaux = this.first;
        if ((this.end() < pos) || (pos < 1)) {
            System.out.println("Wrong position");
            // Lanzar excepción
        }
        for (int i = 1; i < pos - 1; i++) {
            nodeaux = nodeaux.next;
        }
        Node nodesaved = nodeaux.next;
        nodeaux.next = node;
        node.next = nodesaved;
    }

    public boolean empty(){
        if (this.first == null) {
            return true;
        } else {
            return false;
        }
    }

    public int end(){
        /* Devuelve el siguiente al último elemento de la lista,
         * es decir, donde se ha de insertar un nuevo elemento
         */
        int i;
        Node nodeaux = this.first;
        if (nodeaux == null) {
            return 1;
        }
        for (i = 1; nodeaux != this.last; i++) {
            nodeaux = nodeaux.next;
        }
        return i+1;
    }

    public void destroy() {
        /* Erases a list */
        this.first = null;
        this.last = null;
    }

    public Node getElement(int pos) {
        Node nodeaux = this.first;
        if (pos >= this.end() || pos<1) {
            System.out.println("Posición errónea");
            // Lanzar excepción
        }
        for (int i = 1; i < pos; i++){
            nodeaux = nodeaux.next;
        }
        return nodeaux;
    }

    public void delete(int pos) {
        Node nodeaux = this.first;
        if (pos >= this.end() || pos<1) {
            System.out.println("Posición errónea");
            // Lanzar excepción
        }
        if (pos == 1) {
            this.first = this.first.next;
        } else if (pos == this.end() - 1) {
            this.last = this.getElement(this.end() - 2);
            this.last.next = null;
        } else {
            for (int i = 1; i < pos - 1; i++){
                nodeaux = nodeaux.next;
            }
            nodeaux.next = nodeaux.next.next;
        }
    }

    public int nextElement(int pos) {
         if (pos >= this.end() || pos<1) {
            System.out.println("Posición errónea");
            // Lanzar excepción
         }
        return pos + 1;
    }

    public int prevElement(int pos) {
        if (pos >= this.end() || pos<2) {
            System.out.println("Posición errónea");
            // Lanzar excepción
        }
        return pos - 1;
    }

    public int findElement(Node node) {
        Node nodeaux = this.first;
        int i;
        if (this.empty()) {
            System.out.println("¡Lista vacía!");
            // Lanzar excepción
        }
        for (i = 1 ; nodeaux != node && i != this.end(); i++) {
            nodeaux = nodeaux.next;
        }
        if (nodeaux == node) {
            return i;
        } else {
            return this.end();
        }
    }

}
